package com.weex.app.extend;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.LruCache;
import android.widget.ImageView;

//import com.nostra13.universalimageloader.core.ImageLoader;
// http://songfeng-123.iteye.com/blog/2366171 Weex加载Android本地图片

public class ImageAppIco {
    private Context _context;
    LruCache<String, Drawable> _cache;

    private ImageAppIco() {
        _cache = new LruCache<String, Drawable>(128);
    }

    private static ImageAppIco instance = null;

    public static synchronized ImageAppIco getInstance() {
        if (instance == null) {
            instance = new ImageAppIco();
        }
        return instance;
    }


    public void setIco(final String pkg, final ImageView view){
        Drawable ico = _cache.get(pkg);
        if(ico==null){
            PackageManager pm = _context.getPackageManager();

            try {
                ApplicationInfo info = pm.getApplicationInfo(pkg, PackageManager.GET_META_DATA);
                ico = info.loadIcon(pm);
                _cache.put(pkg,ico);
            } catch (PackageManager.NameNotFoundException e) {
            }
        }

        if(ico!=null){
            view.setImageDrawable(ico);
        }
    }

    public void init(Context context) {
        _context=context;
    }
}
